<html>
    <head></head>
    <body>
        Hello Queue
        <script>
            //队列
            function Queue(){
                //队列的节点
                var Node = function(key){
                    this.key = key;
                    this.next = null;
                    this.pre = null;
                }

                //队列的头
                var head = null;
                //队列的尾
                var tail = null;

                //入队
                this.push = function(key){
                    var newNode = new Node(key);
                    console.log(key + " 入队");

                    if(head === null){
                        head = newNode;
                        tail = newNode;
                    }                      
                    else{
                        newNode.next = head;
                        head.pre = newNode;
                        head = newNode;
                    }
                }

                //出队
                this.pop = function(){
                    if(tail === null){
                        console.log("队列已空");
                    }else{
                        console.log(tail.key + " 出队");

                        var temp = tail;                        
                        tail = tail.pre;
                        tail.next = null;

                        temp.next = null;
                        return temp;
                    }
                }

                //遍历
                this.print = function(){
                    var temp = tail;
                    var str = "";
                    while(tail){
                        str += " "+tail.key;
                        tail = tail.pre;
                    }
                    console.log(str);
                    tail = temp;
                    temp = null;
                }

                this.getLength = function(){
                    var len = 0;
                    var temp = tail;
                    while(tail){
                        len++;
                        tail = tail.pre;
                    }
                    tail = temp;
                    temp = null;
                    return len;
                }    

            }

            //测试
            var nodes = [89,34,98,12,88];
            var queue = new Queue();
            var len = nodes.length;
            for(var i=0;i<len;i++){
                queue.push(nodes[i]);
            }
            console.log("-----console------");
            queue.print();
            console.log("------------------");
            queue.pop();
            queue.pop();
            queue.pop();
            console.log("-----console------");
            queue.print();
            console.log("------------------");
            queue.push(1);
            queue.push(2);
            queue.push(3);            
            console.log("-----length-------");
            console.log("队列的长度：" + queue.getLength());
            console.log("-----console------");
            queue.print();
            console.log("------------------");
            queue.pop();
            queue.pop();
            queue.push(77);
            console.log("-----length-------");
            console.log("队列的长度：" + queue.getLength());
            console.log("-----console------");
            queue.print();
        </script>
    </body>
</html>